package com.chen.manager.service.impl;

import cn.hutool.log.Log;
import com.chen.manager.mapper.SysMenuMapper;
import com.chen.manager.mapper.SysMenuRoleMapper;
import com.chen.manager.service.SysMenuRoleService;
import com.chen.manager.service.SysMenuService;
import com.chen.model.dto.system.AssignMenuDto;
import com.chen.model.entity.system.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cgj
 * @date 2/25/25 7:35 PM
 */
@Service
@Transactional
public class SysMenuRoleServiceImpl implements SysMenuRoleService {

    @Autowired
    private SysMenuRoleMapper sysMenuRoleMapper;
    // sysMenuService中已经处理好了树形结构, 这里直接掉service即可
    @Autowired
    private SysMenuService sysMenuService;
    @Transactional(readOnly = true)
    @Override
    public Map<String, Object> showAll(Long id) {
//        //查询所有
        List<SysMenu> sysMenuList = sysMenuService.showAll();
        //查询当前用户拥有的角色
        List<Long>  holdMenuIds= sysMenuRoleMapper.selectHoldMenuId(id);
  //进行封装
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("sysMenuList",sysMenuList);
        resultMap.put("holdMenuIds",holdMenuIds);
        return resultMap;
    }

    @Override
    public void sava(AssignMenuDto assignMenuDto) {
        //先把原来角色有的菜单都删除, 在统一添加, 这样业务更简单
        sysMenuRoleMapper.deleteMenuByRid(assignMenuDto.getRoleId());
        //获取菜单的id
        List<Map<String, Number>> menuIdList = assignMenuDto.getMenuIdList();
        if(menuIdList.size()>0 &&menuIdList!=null){
            //添加
            sysMenuRoleMapper.insertMenu(assignMenuDto);
        }

    }
}
